/*
 * Copyright 2009-2010 Belmont Software Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.belmont.backup.client;
import java.util.*;
import com.google.gwt.core.client.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BackupUI implements EntryPoint, WindowResizeListener, TabListener {
    static final int BRANDING_COMPANY = 0;
    static final int BRANDING_PRODUCT = 1;
    static final int BRANDING_LOGO_URL = 2;
    static final int BRANDING_CLIENT_NAME = 3;
    static final int BRANDING_COMPANY_URL = 4;

    UIPanel uipanels[] = new UIPanel[6];
    BackupAdminInterfaceAsync service;
    Button  versionRefresh = new Button("Refresh");    
    TextArea msgBox = new TextArea();
    DockPanel top = new DockPanel();
    TabPanel tp = new TabPanel();
    String brandingStrings[];

    private BackupAdminInterfaceAsync createService() {
        BackupAdminInterfaceAsync svc = (BackupAdminInterfaceAsync)GWT.create(BackupAdminInterface.class);
        ServiceDefTarget endpoint = (ServiceDefTarget)svc;
        String moduleRelativeURL = GWT.getHostPageBaseURL()+"admin";
        endpoint.setServiceEntryPoint(moduleRelativeURL);

        return svc;
    }

    String getBrandingProperty(int prop, String defaultValue) {
        if (brandingStrings != null && brandingStrings[prop] != null) {
            return brandingStrings[prop];
        } else {
            return defaultValue;
        }
    }

    void refresh() {
        service.getBrandingStrings(new AsyncCallback<String[]>() {
                public void onFailure(Throwable caught) {
                    print("Failed to get branding strings, using default.");
                }
                public void onSuccess(String result[]) {
                    brandingStrings = result;
                    logo.setUrl(brandingStrings[BRANDING_LOGO_URL]);
                    title.setHTML("<h2>"+brandingStrings[BRANDING_COMPANY]+" "+brandingStrings[BRANDING_PRODUCT]+" Admin Console</h2>");
                }
            });

        for (int i=0; i < uipanels.length; i++) {
            if (uipanels[i] != null) {
                uipanels[i].refresh();
            }
        }
    }

    public boolean onBeforeTabSelected(SourcesTabEvents source, int index) {
        return true;
    }

    public void onTabSelected(SourcesTabEvents source, int index) {
        print("");
        if (uipanels[index] != null) {
            uipanels[index].refresh();
        }
    }

    public void onWindowResized(int width, int height) {
        //top.setPixelSize(width, height);
        tp.getDeckPanel().setHeight("95%");
    }

    public void print(String msg) {
        msgBox.setText(msg);
    }

    Image logo;
    HTML title;
    
    public void onModuleLoad() {
        service = createService();
        logo = new Image("Belmont.png");
        HorizontalPanel head = new HorizontalPanel();
        head.add(logo);
        head.add(title = new HTML("<h2>Backup Service Admin Console</h2>"));
        head.setSpacing(6);
        top.add(head, DockPanel.NORTH);

        tp.addTabListener(this);

        uipanels[0] = new AboutPanel(this);
        tp.add(uipanels[0].init(), uipanels[0].getName());

        uipanels[1] = new ServiceOptionsPanel(this);
        tp.add(uipanels[1].init(), uipanels[1].getName());

        uipanels[2] = new ClientOptionsPanel(this);
        tp.add(uipanels[2].init(), uipanels[2].getName());

        uipanels[3] = new BackupsPanel(this);
        tp.add(uipanels[3].init(), uipanels[3].getName());

        uipanels[4] = new LogsPanel(this);
        tp.add(uipanels[4].init(), uipanels[4].getName());

        uipanels[5] = new DocumentationPanel(this);
        tp.add(uipanels[5].init(), uipanels[5].getName());

        refresh();

        tp.selectTab(0);
        tp.setWidth("100%");
        //tp.getDeckPanel().setHeight("400px");
        tp.getDeckPanel().setHeight("95%");
        top.add(tp, DockPanel.CENTER);
        msgBox.setVisibleLines(3);
        msgBox.setText("Status messages");
        msgBox.setReadOnly(true);
        msgBox.setWidth("100%");
        tp.setWidth("100%");
        tp.setHeight("100%");
        top.add(msgBox, DockPanel.SOUTH);
        top.setSpacing(2);
        top.setCellWidth(tp, "100%");
        top.setCellHeight(tp, "100%");
        top.setCellHeight(head, "70px");
        top.setHeight("100%");
        top.setWidth("100%");
        Window.addWindowResizeListener(this);
        RootPanel.get().add(top);
    }
}
